<template>
	<view>
		<view class="flex justify-between align-center padding">
			<view class="flex justify-start align-center">
				<button  class="bg-inherit headimg inline-block" open-type="chooseAvatar"
					@chooseavatar="onChooseAvatar">
					<view v-if="client.headimg" class="cu-avatar xl round" :style="'background-image:url('+client.headimg+');'"></view>
				</button>
				<view class=" flex flex-direction margin-left-lg">
					<view class="flex justify-start align-center text-lg">
						<text class="cuIcon-peoplefill text-main "></text>
						<input class="text-df" type="nickname" v-model="client.nc" placeholder="请输入昵称" />
					</view>
					<view class="flex justify-start align-center text-lg">
						<text class="cuIcon-phone text-main"></text>
						<!-- <input class="text-df" v-model="client.tel" placeholder="请输入电话" /> -->
						<text v-if="client.tel" class="text-sm phone-button">{{client.tel}}</text>
						<button v-else class="phone-button text-sm" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">请输入手机号</button>
					</view>
				</view>
				<view class="relative cu-avatar xl bg-inherit">
					<view v-if="client.tx" class="cu-avatar xl round absolute top0 right0"
						:style="'background-image:url('+client.tx+');'"></view>
					<view v-if="client.tx"
						class="cu-avatar xl round absolute top0 right0 bg-cover"></view>
					<view 
						class="bg-inherit cu-avatar xl absolute top0 right0 flex justify-center align-center w-100">
						<view @click="openTx" :class="client.tx ? 'text-red': 'text-main'" class="text-sm flex flex-direction justify-center align-center">
							<text>人脸</text>
							<text>{{client.tx ? "已设置" : "未设置"}}</text>
						</view>
					</view>
				</view>
			</view>
			
			<view style="height: 128rpx; width: auto;" class="text-xxl text-main relative">
				<text @click="save" class="cuIcon-edit absolute bottom0 right0"></text>
			</view>
		</view>
	</view>
</template>

<script>
	import upload from "@/wx-cos/upload.js";
	export default {
		data: function() {
			return {
				txurl: "",
				client: this.store.state.client,
			};
		},
		onShareAppMessage: function() {
			return {
				title: this.store.state.title,
				path: '/pages/first?id=' + this.store.state.client.clientid
			}
		},
		created: function() {
			this.api("/client/info/get").send({
				clientid: this.client.clientid,
			}).then(res => {
				this.client = res.client;
			});
		},
		methods: {
			getPhoneNumber: function(e) {
				let phoneCode = e.detail.code;
				
				if (phoneCode) {
					this.api("/login/phone").send({
						clientid: this.client.clientid,
						jlid: this.store.state.jl.jlid,
						phoneCode,
					}).then(data => {
						this.store.commit("submitClient", data.client);
						this.store.commit("submitDz", data.adz);
						this.store.commit("submitJl", data.ajiaolian);
						this.client = data.client;
						this.message.info("电话设置成功");
					});
				}

			},
			onChooseAvatar: function(e) {
				// 上传图片
				// 得到图片云服务地址
				upload.checkUploadFile(
					e.detail.avatarUrl,
					this.client.clientid,
					"headimg", pro => {}
				).then(data => {
					this.client.headimg = data.url;
					this.$forceUpdate();
				});
			},
			openTx: function() {
				uni.chooseMedia({
					count: 1,
					mediaType: ['image'],
					sourceType: ['album', 'camera'],
					sizeType: ['original'],
					camera: "back",
					success: res => {
						upload.checkUploadFile(
							res.tempFiles[0].tempFilePath,
							this.client.clientid,
							"face", progressData => {
								console.log(progressData);
							}).then((data) => {
							this.client.tx = data.url;
							this.save();
						});
					}
				})
			},
			save: function() {
				this.api("/client/info/put").send({
					client: this.client
				}).then(res => {
					this.store.commit("submitClient", this.client);
					this.message.info("修改成功");
				});
			}
		}
	}
</script>

<style scoped>
	.headimg {
		padding-left: 0;
		padding-right: 0;
	}

	.headimg::after {
		border: none;
	}
	
	.phone-button {
		border-style: none;
		background-color: rgba(0, 0, 0, 0);
		padding: 0 0 0 5px;
		margin: 0;
	}
	
	.phone-button::after {
		border-style: none;
		background-color: rgba(0, 0, 0, 0);
	}
</style>